package cn.marinda.config;

import cn.marinda.util.FileUtils;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 摘要设置配置
 *
 * @author Marinda
 * @date 2022/07/07
 */
@Data
public abstract class AbstractSettingConfiguration implements SettingConfiguration{
    protected File fileConfig;
    protected String fileName;
    protected String type;
    protected File fileDir;
    protected String filePrefix;
    protected Map<String,Object> settingMap = new HashMap<>();
    /**
     * 摘要设置配置
     */
    public AbstractSettingConfiguration(){

    }
    /**
     * 摘要设置配置
     *
     * @param fileName   文件名称
     * @param type       类型
     */
    public AbstractSettingConfiguration(String fileName,String type,String filePrefix){
        this.fileName = fileName;
        this.type = type;
        this.filePrefix = filePrefix;
        createFileDir();
        createConfigFile();
        loadFileInfo();
    }


    /**
     * 加载文件信息
     */
    public void loadFileInfo(){
        if(!isFileReadText()){
            writeSetting();
            writeSetting(loadContextList(),settingMap);
        }
        List<String> readList = readFileContent();
        writeSetting(readList,settingMap);
    }

    /**
     * 如果文件里有内容则不进行写入
     */
    protected boolean isFileReadText(){
        boolean flag = false;
        try {
            FileInputStream inputStream = new FileInputStream(fileConfig);
            flag = inputStream.read() != -1 ? true : false;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 获取文件内容
     *
     * @return boolean
     */
    protected List<String> readFileContent(){
        List<String> textList = new ArrayList<>();
        try {
            FileInputStream inputStream = new FileInputStream(fileConfig);
            byte[] bytes = new byte[inputStream.available()];
            int len;
            while((len = inputStream.read(bytes)) != -1){
                String text = new String(bytes);
                textList.add(text);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return textList;
    }

    /**
     * 往一个hashmap里面写入相关配置数据
     *
     * @param textList   文本列表
     * @param settingMap 设置地图
     */
    public void writeSetting(List<String> textList, Map<String,Object> settingMap){
        for(String str : textList){
//            跳过注释
            if(str.startsWith("#") || str.startsWith("[")){
                continue;
            }
            int splitIndex = str.indexOf("=");
//           key
            String key = str.substring(0,splitIndex);
//           value
            boolean value = Boolean.parseBoolean(str.substring(splitIndex+1));
            if(settingMap.containsKey(key)){
                continue;
            }
            settingMap.put(key,value);
        }
    }


    @Override
    public void writeSetting() {
        try {
            FileOutputStream outputStream = new FileOutputStream(fileConfig);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            List<String> textList = loadContextList();
            for(String text : textList){
                FileUtils.appendFileTextLine(bufferedWriter,text);
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 读取设置
     *
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    public abstract Map<String,Object> readSetting();

    /**
     * 创建文件目录
     *
     * @return boolean
     */
    private boolean createFileDir(){
        fileDir = new File("./setting");
        if(fileDir.isDirectory()){
            if(fileDir.exists()){
                return false;
            }
        }
        fileDir.mkdir();
        return true;
    }

    /**
     * 创建配置文件
     *
     * @return {@link File}
     */
    @Override
    public boolean createConfigFile() {
            boolean flag = false;
            fileConfig = new File(fileDir,fileName +"."+ filePrefix);
            //        已存在
            if(fileConfig.exists()) {
                flag = false;
            }
            try {
                fileConfig.createNewFile();
                flag = true;
            }catch (Exception e){
                e.printStackTrace();
            }
        return flag;
    }

    /**
     * 得到类型
     *
     * @return {@link String}
     */
    @Override
    public String getType() {
        return type;
    }

}
